<template>
  <div>
    <transition name="slide-up">
      <div class="setting-wrapper" v-show="menuVisible && settingVisible === 2">
        <!-- 位置设置 -->
        <div class="setting-progress">
          <div class="read-time-wrapper" @click="prevSection()">
            <span class="read-time-text">{{ getReadTimeText() }}</span>
            <span class="icon-forward"></span>
          </div>
          <div class="progress-wrapper">
            <div class="progress-icon-wrapper">
              <span class="icon-back"></span>
            </div>
            <input
              class="progress"
              type="range"
              max="100"
              min="0"
              step="1"
              :value="progress"
              @change="onProgressChange($event.target.value)"
              @input="onProgressInput($event.target.value)"
              :disabled="!bookAvailable"
              ref="progress"
            />
            <div class="progress-icon-wrapper" @click="nextSection()">
              <span class="icon-forward"></span>
            </div>
            <div class="text-wrapper">
              <span class="progress-section-text">{{ getSectionName }}</span>
              <span>({{ bookAvailable ? progress + '%' : '加载中...' }})</span>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import { ebookMixin } from '@/utils/mixin.js'
export default {
  mixins: [ebookMixin],
  methods: {
    onProgressChange(progress) {
      this.setProgress(progress).then(() => {
        this.displayProgress()
        this.updateProgressBg()
      })
    },
    onProgressInput(progress) {
      this.setProgress(progress).then(() => {
        this.updateProgressBg()
      })
    },
    // 定位进度
    displayProgress() {
      const cfi = this.currentBook.locations.cfiFromPercentage(
        this.progress / 100
      )
      //   this.currentBook.rendition.display(cfi)
      this.display(cfi)
    },
    // 触发时改变进度条背景
    updateProgressBg() {
      this.$refs.progress.style.backgroundSize = `${this.progress}% 100%`
      this.$refs.progress.style.cssText = `background-size:${this.progress}% 100% !important`
    },
    // 上下章切换 start
    prevSection() {
      if (this.section > 0 && this.bookAvailable) {
        this.setSection(this.section - 1).then(() => {
          this.displaySection()
        })
      }
    },
    nextSection() {
      if (
        //   阅读器进度 spine
        this.section < this.currentBook.spine.length - 1 &&
        this.bookAvailable
      ) {
        this.setSection(this.section + 1).then(() => {
          this.displaySection()
        })
      }
    },
    // 上下章切换 end
    displaySection() {
      const sectionInfo = this.currentBook.section(this.section)
      console.log(sectionInfo)
      if (sectionInfo && sectionInfo.href) {
        // 通过book的rendition.display方法跳转
        // this.currentBook.rendition.display(sectionInfo.href).then(() => {
        //   this.refreshLocation()
        //   this.updateProgressBg()
        // })
        this.display(sectionInfo.href)
        console.log(this.progress + '当前的progress')
      }
    }
  },
  computed: {
    // book.navigation.get(sectionInfo.href).label来获取章节名称，
    // 这个章节名称加在计算属性里，每次值变化的时候就会自动更新
    // 这里不能获取二级目录信息 注释掉
    // getSectionName() {
    //   if (this.section) {
    //     const sectionInfo = this.currentBook.section(this.section)
    //     if (sectionInfo && sectionInfo.href) {
    //       return this.currentBook.navigation.get(sectionInfo.href).label
    //     }
    //   }
    //   return ''
    // }
    getSectionName() {
      return this.section ? this.navigation[this.section].label : ''
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../assets/style/global.scss';
.setting-wrapper {
  position: absolute;
  left: 0;
  bottom: px2rem(30);
  z-index: 101;
  width: 100%;
  height: px2rem(90);
  background: white;
  box-shadow: 0 px2rem(-8) px2rem(8) rgba(0, 0, 0, 0.15);
  .setting-font-size {
    display: flex;
    height: 100%;
    .preview {
      flex: 0 0 px2rem(40);
      @include center;
    }
    .select {
      display: flex;
      flex: 1;
      .select-wrappr {
        flex: 1;
        display: flex;
        align-items: center;
        &:first-child {
          .line {
            &:first-child {
              border-top: none;
            }
          }
        }
        &:last-child {
          .line {
            &:last-child {
              border-top: none;
            }
          }
        }
        .line {
          flex: 1;
          height: 10px;
          border-top: px2rem(1) solid #ccc;
        }
        .point-wrapper {
          // 表示没有宽度的
          position: relative;
          flex: 0 0 0;
          width: 0;
          height: px2rem(7);
          border-left: px2rem(1) solid #ccc;
          .point {
            position: absolute;
            top: px2rem(-10);
            left: px2rem(-10);
            background: white;
            width: px2rem(20);
            height: px2rem(20);
            border-radius: 50%;
            border: px2rem(1) solid #ccc;
            box-shadow: 0 px2rem(4) px2rem(4) rgba(0, 0, 0, 0.15);
            @include center;
            .small-point {
              width: px2rem(5);
              height: px2rem(5);
              background: black;
              border-radius: 50%;
            }
          }
        }
      }
    }
  }
  .setting-theme {
    display: flex;
    height: 100%;
    .setting-theme-item {
      display: flex;
      flex: 1;
      margin: px2rem(2) px2rem(2);
      flex-direction: column;
    }
    .preview {
      flex: 1;
      border: 1px solid #ccc;
      &.no-border {
        border: none;
      }
    }
    .text {
      flex: 1;
      font-size: px2rem(10);
      color: #ccc;
      @include center;
      &.selected {
        color: #333;
      }
    }
  }
  .setting-progress {
    position: relative;
    width: 100%;
    height: 100%;
    .read-time-wrapper {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: px2rem(40);
      font-size: px2rem(12);
      @include center;
    }
    .progress-wrapper {
      width: 100%;
      height: 100%;
      @include center;
      box-sizing: border-box;
      padding: 0 px2rem(20);
      .progress {
        width: 100%;
        -webkit-appearance: none;
        height: px2rem(1);
        // 设置两种背景 也就是滑动体 左侧是灰色 右侧是白色
        background: -webkit-linear-gradient(#999, #999) no-repeat, #ddd;
        // 让两种颜色进行切换 让js控制
        // 当 0 100% 也就是前面的#ddd覆盖 #999
        background-size: 0 100%;
        margin: 0 px2rem(8);
        &:focus {
          outline: none;
        }
        &::-webkit-slider-thumb {
          -webkit-appearance: none;
          height: px2rem(15);
          width: px2rem(15);
          border-radius: 50%;
          background: white;
          box-shadow: 0 4px 4px 0 rgba(0, 0, 0, 0.15);
          border: px2rem(1) solid #ddd;
        }
      }
      .text-wrapper {
        position: absolute;
        width: 100%;
        left: 0;
        bottom: px2rem(14);
        font-size: px2rem(9);
        // text-align: center;
        @include center;
        padding: 0 px2rem(15);
        box-sizing: border-box;
        .progress-section-text {
          @include ellipsis;
        }
      }
    }
    .progress-icon-wrapper {
      padding-top: px2rem(1);
      font-size: px2rem(16);
    }
  }
}
</style>
